/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月15日   下午5:23:44   
 */
package com.huacheng.cms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huacheng.cms.dao.CommentMapper;
import com.huacheng.cms.model.Comment;
import com.huacheng.cms.model.vo.CommentVo;

@Service
public class CommentService {
	@Autowired
	private CommentMapper commentM;

	public void addOne(Comment comment) {
		comment.setCreatetime(new Date());
		commentM.insertSelective(comment);
	}

	public Comment getOneByKey(long commentid) {
		return commentM.selectByPrimaryKey(commentid);
	}

	public int deleteOneByKey(long commentid) {
		Comment comment = new Comment();
		comment.setParentid(commentid);
		List<CommentVo> list = commentM.selectBySelective(comment, 0, 0);
		for (CommentVo c : list) {
			deleteOneByKey(c.getCommentid());
		}
		return commentM.deleteByPrimaryKey(commentid);
	}

	public int updateOneByObj(Comment comment) {
		comment.setCreatetime(new Date());
		return commentM.updateByPrimaryKeySelective(comment);
	}

	public int getListCountByObj(Comment comment) {
		return commentM.selectBySelectiveCount(comment);
	}

	public List<CommentVo> getListByObj(Comment comment, long offset, long rows) {
		return commentM.selectBySelective(comment, offset, rows);
	}

	public List<CommentVo> getListOfObjTree(Comment comment) {
		List<CommentVo> list = commentM.selectBySelective(comment, 0, 0);
		for (CommentVo cv : list) {
			cv.setChildren(getChildren(cv.getCommentid()));
		}
		return list;
	}

	public List<CommentVo> getChildren(long parentid) {
		if (parentid == 0)
			return null;
		long offset, rows;
		offset = rows = 0;
		Comment comment = new Comment();
		comment.setParentid(parentid);
		List<CommentVo> list = commentM.selectBySelective(comment, offset, rows);
		for (CommentVo cv : list) {
			cv.setChildren(getChildren(cv.getCommentid()));
		}
		return list;
	}
}
